<template>
<el-container class="home-container">
  <el-header>
  <div class="left">
    <img src="../assets/logo1.png" alt="">
    <span>电商后台管理系统</span>
  </div>
  <el-button type="info" @click="logOut">退出</el-button>
  </el-header>
  <el-container>
    <!-- 一定要中间空一格？？ -->
    <el-aside :width="isCollapse ? '64px':'200px'">
      <div class="toggle" @click="toggleCollapse">|||</div>
     <el-menu
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409bff" unique-opened
      :collapse="isCollapse"
      :collapse-transition="false" router
      :default-active="active">
      <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
        <template slot="title">
          <i :class="iconList[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
          <el-menu-item :index="'/'+subItem.path"
          v-for="subItem in item.children"
          :key="subItem.id"
          @click="saveNavstate('/'+subItem.path)">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </template>
            </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <el-main>
      <!-- 二级路由占位 -->
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>
<script>
export default {
  name: 'HomePage',
  data () {
    return {
      menuList: [],
      iconList: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        145: 'iconfont icon-danju',
        102: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      active: '' // 高亮状态的保持
    }
  },
  methods: {
    logOut () {
      // 没有token会强制跳转
      // 所以清空 token 就能实现推出功能
      // 强制跳转是因为清空时没有没有页面跳转  路由守卫无法实现强制跳转  因此得多加一行跳转代码
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      // console.log(this.menuList)
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavstate (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.active = activePath
    }
  },
  created () {
    this.getMenuList()
    this.active = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    // align-content: center;
    align-items: center;
    padding-left: 0;
    .left {
      display: flex;
      align-items: center; // 默认是strench 拉伸以适应容器
      >img {
        margin-right: 10px;
      }
      span {
        color: white;
      }
    }
}
.el-aside {
  background-color: #333744;
  // font-size: 14px; 设不灵泛
  .toggle {
    color: white;
    background-color: #4a5064;
    font-size: 12px;
    line-height: 28px;
    text-align: center;
    letter-spacing: .2em;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border: 0;
}
}

</style>
